﻿#include "gamecontrol.h"

#include <QDebug>
#include <QRandomGenerator>
#include <QTimer>

#include "playhand.h"

GameControl::GameControl(QObject* parent) : QObject(parent) {}

void GameControl::playerInit() {

	// 对象实例化
	m_Left = new UserPlayer("我左边", this);
	m_Right = new UserPlayer("我右边", this);
	m_user = new UserPlayer("我自己", this);
	m_Top = new UserPlayer("我上边", this);

	// 头像的显示位置
	m_Left->setDirection(Player::Left);
	m_Right->setDirection(Player::Right);
	m_user->setDirection(Player::Right);
	m_Top->setDirection(Player::Left);

	// 设置性别（使用随机数）
	Player::Sex sex;
	sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
	m_Left->setSex(sex);
	sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
	m_Right->setSex(sex);
	sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
	m_user->setSex(sex); 
	sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
	m_Top->setSex(sex);

	// 出牌顺序 上家下家
	// user
	m_user->setPrevPlayer(m_Left);
	m_user->setNextPlayer(m_Right);

	// left 玩家
	m_Left->setPrevPlayer(m_Top);
	m_Left->setNextPlayer(m_user);

	// right 玩家
	m_Right->setPrevPlayer(m_user);
	m_Right->setNextPlayer(m_Top);

	// Top 玩家
	m_Top->setPrevPlayer(m_Right);
	m_Top->setNextPlayer(m_Left);


	// 指定当前玩家为真人玩家
	m_currPlayer = m_user;
}

UserPlayer* GameControl::getUserPlayer() { return m_user; }
UserPlayer* GameControl::getLeft() { return m_Left; }
UserPlayer* GameControl::getRight() { return m_Right;  }
UserPlayer* GameControl::getTop() { return m_Top; }
void GameControl::setCurrentPlayer(Player* player) { m_currPlayer = player; }

Player* GameControl::getCurrentPlayer() { return m_currPlayer; }


// 初始化一副牌（没有王）
void GameControl::initAllCards() {
	m_allCards.clear();  //清空
	for (int p = Card::Card_Begin + 1; p < Card::Card_SJ; ++p)  //每一个点数对应四个花色
	{
		for (int s = Card::Suit_Begin + 1; s < Card::Suit_End; ++s)  //枚举不能直接算数运算
		{
			Card c((Card::CardPoint)p, (Card::CardSuit)s);  //强制转换回枚举类型
			m_allCards.add(c);
		}
	}
}

// 每次发一张牌
Card GameControl::takeOneCard() {
	// 调用了cards类的随机选取一张牌的函数 
	return m_allCards.takeRandomCard();
}

// 得到剩下牌的数量，用来停止发牌
int GameControl::getCardsNumber() {
	return m_allCards.cardCount();
}

// 重新开始，洗牌
void GameControl::resetCardData() {
	// 洗牌
	initAllCards();
	// 清空所有玩家的牌
	m_Left->clearCards();
	m_Right->clearCards();
	m_user->clearCards();
	m_Top->clearCards();
}

// 成为地主
void GameControl::becomeLord(Player* player) {
	player->setRole(Player::Lord);  //设置角色 地主 农民
	player->getPrevPlayer()->setRole(Player::Farmer);
	player->getNextPlayer()->setRole(Player::Farmer);
	player->getNextPlayer()->getNextPlayer()->setRole(Player::Farmer);

	m_currPlayer = player;  //设置当前玩家优先出牌

	QTimer::singleShot(1000, this,
		[=]()  //延时触发，游戏状态改变，玩家状态改变，玩家准备出牌
		{
			emit gameStatusChanged(PlayingHand);
		});
}

// 一轮结束了，清空分数
void GameControl::clearPlayerScore() {
	m_Left->setScore(0);
	m_Right->setScore(0);
	m_user->setScore(0);
	m_Top->setScore(0);
}

void GameControl::setStartScore() {
	m_Left->setScore(50);
	m_Right->setScore(50);
	m_user->setScore(50);
	m_Top->setScore(50);
}

// 得到目前最大的下注点数
int GameControl::getPlayerMaxBet() { return max_bet; }
